<?php
    $scriptId = "am-$type-" . filterId($this->id);
    $divId = "div-".$scriptId;
    $rootUrl = $isSecure ? ROOT_SURL : ROOT_URL;
    $flowPlayerJs = $rootUrl . "/application/default/views/public/js/flowplayer/flowplayer.js";
    $flowPlayerIpadJs = $rootUrl . "/application/default/views/public/js/flowplayer/flowplayer.ipad.js";
    $flowPlayerSwf = $rootUrl . "/application/default/views/public/js/flowplayer/flowplayer.swf";
?>
<?php if (!empty($media)) : // user has access to media ?>
jQuery(document).ready(function($) {
    var runMedia = function()
    {
        var $a = $("<a id='<?php p($divId)?>'></a>");
        $a.attr("href","<?php p($media);?>");
        $a.css({
            width : '<?php p($width)?>',
            height : '<?php p($height)?>',
            display: 'block'
        });
        $("script#<?php echo $scriptId?>").after($a);
        flowplayer('<?php p($divId)?>', '<?php p($flowPlayerSwf)?>', <?php echo(empty($flowPlayerParams) ? '{}' : json_encode($flowPlayerParams)) ?>).ipad();
    }
    // if !flowplayer loaded, load it in async mode
    if (typeof flowplayer == 'function')
    {
        runMedia();
    } else {
        jQuery.getScript('<?php p($flowPlayerJs)?>', function(){
            jQuery.getScript('<?php p($flowPlayerIpadJs)?>', runMedia)
        });
    }
});
<?php else: // no media ?>
jQuery(document).ready(function($) {
    var $a = $("<a id='<?php p($divId)?>'></a>");
    $a.attr("href",<?php echo json_encode($link);?>);
    $a.css({
        width : '<?php p($width)?>',
        height : '<?php p($height)?>',
        display: 'block',
        'background-color' : '#aaa'
    });
    $a.text('<?php p($error)?>');
    $("script#<?php echo $scriptId?>").after($a);
});
<?php endif; ?>